//
//  MTUserApiManager.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/5.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTUserApiManager.h"
#import <JPush/JPUSHService.h>
static MTUserApiManager *usermanger = nil;
@implementation MTUserApiManager

+(MTUserApiManager *)manager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        usermanger = [[MTUserApiManager alloc] init];
    });
    return usermanger;
    
}
/**
 获取注册的验证码
 
 @param phone 手机号
 @param countryCode 地区编码
 @param completionBlock 完成block
 */
-(void)getregisterVerCode:(NSString *)phone
              countryCode:(NSString *)countryCode
               completion:(void (^)(MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"countryCode":countryCode,@"phone":phone};
    [[MTHttpTool shareTool] getPath:@"/user/get_register_verify_code" parameters:param success:^(MTBaseData *datamodel) {
        completionBlock(nil);
    } failer:^(MTError *error) {
        completionBlock(error);
    }];
}

/**
 用户登录
 
 @param phone 登录手机号
 @param password 密码
 @param completionBlock 完成block
 */
-(void)loginWithphone:(NSString *)phone
             password:(NSString *)password
          countryCode:(NSString *)countryCode
           completion:(void (^)(MTUserModel* user, MTError *mterror))completionBlock
{
    NSString *crypPassword =    [LTAESTool decryptAES:password key:passwordCrypkey kVector:passwordCrypInvterKey];
    
    NSString *newCountryCode = [countryCode stringByReplacingOccurrencesOfString:@"+" withString:@""];
    
    
    NSDictionary *param = @{@"phone":phone,@"password":crypPassword,@"countryCode":newCountryCode};
    [[MTHttpTool shareTool] postPath:@"/user/login" parameters:param success:^(MTBaseData *datamodel) {
        MTUserModel *model = [MTUserModel mj_objectWithKeyValues:datamodel.data];
        [MTUserModel saveCurrentUser:model];
        [JPUSHService setAlias:model.userId completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
        } seq:1];
        completionBlock(model,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}

/**
 用户注册
 
 @param phone 手机号
 @param countryCode 地区编码
 @param verifyCode 验证码
 @param type 注册类型 ，手机 1 ，三方
 @param avatar 头像
 @param name 名字
 @param password 密码
 @param gender 性别 1 男 2 女
 @param completionBlock 完成block
 */
-(void)registerUser:(NSString *)phone
        countryCode:(NSString *)countryCode
         verifyCode:(NSString *)verifyCode
               type:(NSString *)type
             avatar:(NSString *)avatar
               name:(NSString *)name
           password:(NSString *)password
             gender:(NSString *)gender
         completion:(void (^)(MTUserModel* user, MTError *mterror))completionBlock
{
        NSString *crypPassword =    [LTAESTool decryptAES:password key:passwordCrypkey kVector:passwordCrypInvterKey];
        NSString *newCountryCode = [countryCode stringByReplacingOccurrencesOfString:@"+" withString:@""];
        NSMutableDictionary *param =  [NSMutableDictionary dictionaryWithDictionary:@{@"phone":phone,
                                                                                  @"countryCode":newCountryCode,
                                                                                  @"type":type,
                                                                                  @"verifyCode":verifyCode,
                                                                                  @"password":crypPassword,
                                                                                  }];
    if(name.length != 0)
    {
        param[@"name"] = name;
    }
    if(avatar.length != 0)
    {
        param[@"avatar"] = avatar;
    }
    if(gender.length != 0)
    {
        param[@"gender"] = gender;
    }
    [[MTHttpTool shareTool] postPath:@"/user/register" parameters:param success:^(MTBaseData *datamodel) {
        MTUserModel *model = [MTUserModel mj_objectWithKeyValues:datamodel.data];
        [MTUserModel saveCurrentUser:model];
        [JPUSHService setAlias:model.userId completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
        } seq:1];
        
        
        
        completionBlock(model,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}
/**
 更新头像
 
 @param avatar 头像url
 @param completionBlock 完成block
 */
-(void)updateAvatar:(NSString *)avatar
         completion:(void (^)(MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"avatar":avatar};
    [[MTHttpTool shareTool] postPath:@"/user/update_avatar" parameters:param success:^(MTBaseData *datamodel) {
        completionBlock(nil);
    } failer:^(MTError *error) {
        completionBlock(error);
    }];
}
/**
 获取对应userId的info
 
 @param userId id
 @param completionBlock 完成block
 */
-(void)getUserInfo:(NSString *)userId
        completion:(void (^)(MTUserModel* user, MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"userId":userId};
    [[MTHttpTool shareTool] getPath:@"/user/user_info" parameters:param success:^(MTBaseData *datamodel) {
        MTUserModel *model = [MTUserModel mj_objectWithKeyValues:datamodel.data];
        completionBlock(model,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}

/**
 关注或者取消关注user
 
 @param userId userId
 @param completionBlock 完成block
 */
-(void)followUserWithUserId:(NSString *)userId
                 completion:(void (^)(MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"userId":userId};
    [[MTHttpTool shareTool] postPath:@"/user/follow_user" parameters:param success:^(MTBaseData *datamodel) {
        completionBlock(nil);
    } failer:^(MTError *error) {
        completionBlock(error);
    }];
}

/**
 关注或者取消关注user
 
 @param userId userId
 @param  follow follow
 @param completionBlock 完成block
 */
-(void)followUserV2WithUserId:(NSString *)userId
                       follow:(BOOL)follow
                   completion:(void (^)(MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"userId":userId,
                            @"follow":@(follow)
                            };
    [[MTHttpTool shareTool] postPath:@"/user/follow" parameters:param success:^(MTBaseData *datamodel) {
        completionBlock(nil);
    } failer:^(MTError *error) {
        completionBlock(error);
    }];
    
}



/**
 获取未读消息数量
 
 @param completionBlock 完成
 */
-(void)getCurrentDynamicCount:(void (^)(int count, MTError *mterror))completionBlock
{
    [[MTHttpTool shareTool] getPath:@"/user/get_current_dynamic_cnt" parameters:nil success:^(MTBaseData *datamodel) {
        int unreadCount = [datamodel.data intValue];
        completionBlock(unreadCount,nil);
    } failer:^(MTError *error) {
        completionBlock(0,error);
    }];
}

/**
 获取用户认证信息
 
 @param completionBlock block
 */
-(void)getUserAuthenticationinfoWithcompletion:(void (^)(UserAuthenticationinfo* info, MTError *mterror))completionBlock
{
    [[MTHttpTool shareTool] getPath:@"/user/get_authentication_info" parameters:nil success:^(MTBaseData *datamodel) {
        UserAuthenticationinfo *info = [UserAuthenticationinfo mj_objectWithKeyValues:datamodel.data];
        completionBlock(info,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}

/**
 获取黑名单列表

 @param completionBlock 完成block
 */
-(void)getBlackUserListPage:(int)page
               masterUserId:(NSString *)masterUserId
                 complation:(void (^)(MTUserLiveTotalModel *totalModel, MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"page":[NSString stringWithFormat:@"%d",page],
                            @"masterUserId":masterUserId};
    [[MTHttpTool shareTool] getPath:@"/user/black_user_list" parameters:param success:^(MTBaseData *datamodel) {
        MTUserLiveTotalModel *model =  [MTUserLiveTotalModel mj_objectWithKeyValues:datamodel.data];
        completionBlock(model,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}

/**
 获取禁言列表
 
 @param completionBlock 完成block
 */
-(void)getnoSpeakUserListPage:(int)page
                 masterUserId:(NSString *)masterUserId
                   complation:(void (^)(MTUserLiveTotalModel *totalModel, MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"page":[NSString stringWithFormat:@"%d",page],
                            @"masterUserId":masterUserId};
    [[MTHttpTool shareTool] getPath:@"/user/nospeaking_user_list" parameters:param success:^(MTBaseData *datamodel) {
        MTUserLiveTotalModel *model = [MTUserLiveTotalModel mj_objectWithKeyValues:datamodel.data];
        completionBlock(model,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}

/**
 获取管理员列表
 
 @param completionBlock 完成block
 */
-(void)getMamgerUserList:(void (^)(NSMutableArray<LTUser *>*managerUsers, MTError *mterror))completionBlock
{
    [[MTHttpTool shareTool] getPath:@"/user/user_room_control_list" parameters:nil success:^(MTBaseData *datamodel) {
        NSMutableArray *tempArray = [LTUser mj_objectArrayWithKeyValuesArray:datamodel.data];
        completionBlock(tempArray,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}

/**
 主播添加直播间的管理员

 @param targetUserId 目标用户id
 @param completionBlock 完成block
 */
-(void)addLiveRoomManager:(NSString *)targetUserId
               completion:(void (^)(MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"targetUserId":targetUserId};
    [[MTHttpTool shareTool] postPath:@"/user/add_user_room_control" parameters:param success:^(MTBaseData *datamodel) {
        completionBlock(nil);
    } failer:^(MTError *error) {
        completionBlock(error);
    }];
}


/**
 删除禁言

 @param targetUserId 目标userid
 @param completionBlock 完成block
 */
-(void)deleteNoSpeaktargetUserId:(NSString *)targetUserId
                    masterUserId:(NSString *)masterUserId
                      completion:(void (^)(MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"targetUserId":targetUserId,
                            @"masterUserId":masterUserId
                            };
    [[MTHttpTool shareTool] postPath:@"/user/delete_no_speaking_user" parameters:param success:^(MTBaseData *datamodel) {
        completionBlock(nil);
    } failer:^(MTError *error) {
        completionBlock(error);
    }];
}



/**
 删除管理员
 
 @param targetUserId 目标userid
 @param completionBlock 完成block
 */
-(void)deleteLiveRoomManagertargetUserId:(NSString *)targetUserId
                              completion:(void (^)(MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"targetUserId":targetUserId};
    [[MTHttpTool shareTool] postPath:@"/user/delete_user_room_control" parameters:param success:^(MTBaseData *datamodel) {
        completionBlock(nil);
    } failer:^(MTError *error) {
        completionBlock(error);
    }];
}


/**
 删除黑名单用户

 @param targetUserId 目标userId
 @param masterUserId 主播UserId
 @param completionBlock 完成block
 */
-(void)deleteBlackUsertargetUserId:(NSString *)targetUserId
                      masterUserId:(NSString *)masterUserId
                        completion:(void (^)(MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"targetUserId":targetUserId,
                            @"masterUserId":masterUserId
                            };
    [[MTHttpTool shareTool] postPath:@"/user/delete_black_user" parameters:param success:^(MTBaseData *datamodel) {
        completionBlock(nil);
    } failer:^(MTError *error) {
        completionBlock(error);
    }];
    
}


//获取用户权限
-(void)getUserOperInfoMasterUserId:(NSString *)masterUserId
                      targetUserId:(NSString *)targetUserId
                        completion:(void (^)(MTLiveUserInfo* user,MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"masterUserId":masterUserId,
                            @"targetUserId":targetUserId};
    [[MTHttpTool shareTool] getPath:@"/user/user_oper_info" parameters:param success:^(MTBaseData *datamodel) {
        MTLiveUserInfo *model = [MTLiveUserInfo mj_objectWithKeyValues:datamodel.data];
        completionBlock(model,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
}

/**
 获取自己在该主播直播间的状态
 
 @param masterUserId 主播id
 @param completionBlock 完成block
 */
-(void)getSelfLiveStatusMasterUserId:(NSString *)masterUserId
                          completion:(void (^)(MTUserModel *user, MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"masterUserId":masterUserId};
    [[MTHttpTool shareTool] getPath:@"/user/user_live_status" parameters:param success:^(MTBaseData *datamodel) {
        MTUserModel *model = [MTUserModel mj_objectWithKeyValues:datamodel.data];
        completionBlock(model,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
    
}

/**
 搜索用户添加管理员
 
 @param key 关键字
 @param page 页数
 @param completionBlock 完成block
 */
-(void)searchUserWithKeyWord:(NSString *)key
                        page:(int)page
                  completion:(void (^)(MTUserTotalModel *totalUser, MTError *mterror))completionBlock
{
    NSDictionary *param = @{@"name":key,
                            @"page":[NSString stringWithFormat:@"%d",page]};
    [[MTHttpTool shareTool] getPath:@"/user/user_list" parameters:param success:^(MTBaseData *datamodel) {
        MTUserTotalModel *model = [MTUserTotalModel mj_objectWithKeyValues:datamodel.data];
        completionBlock(model,nil);
    } failer:^(MTError *error) {
        completionBlock(nil,error);
    }];
    
}

@end
